package com.whq.knowverywell.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.whq.knowverywell.R;
import com.whq.knowverywell.business.db.TblUserHandler;
import com.whq.knowverywell.business.model.User;
import com.whq.knowverywell.util.TipUtil;

/**
 * 添加帐号信息
 * @author wanghuaiqiang
 *
 */
public class AddAccountActivity extends BaseLoginActivity {
	private EditText mEtGroupCode = null;
	private EditText mEtUserName = null;
	private EditText mEtPassword = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(R.style.Theme_Sherlock_Light_NoActionBar);
		setContentView(R.layout.add_account);
		
		ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
		ivBack.setOnClickListener(this);
		
		ImageView ivAddAccount = (ImageView) findViewById(R.id.iv_add_account);
		ivAddAccount.setOnClickListener(this);
		
		mEtGroupCode = (EditText) findViewById(R.id.et_groupCode);
		mEtUserName = (EditText) findViewById(R.id.et_userName);
		mEtPassword = (EditText) findViewById(R.id.et_password);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		
		case R.id.iv_add_account:
			this.saveNewUser();
			break;
		}
	}
		
	private void saveNewUser() {
		String groupCode = mEtGroupCode.getText().toString();
		String userName = mEtUserName.getText().toString();
		String password = mEtPassword.getText().toString();
		
		TblUserHandler tblUserHandler = new TblUserHandler(this);
		User oneUser = new User();
		oneUser.setGroupCode(groupCode);
		oneUser.setUserName(userName);
		oneUser.setPassword(password);
		
		tblUserHandler.addOrUpdUser(oneUser);
		TipUtil.tipDescription(this, "添加成功！");
		this.finish();
	}
}
